﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Xml.Linq;

namespace FortnoxAPI
{
    /// <summary>
    /// Get Account from Fortnox
    /// </summary>
    public partial class GetAccount
    {
        /// <summary>
        /// Gets the values.
        /// </summary>
        /// <param name="url">The URL.</param>
        /// <param name="token">The token.</param>
        /// <param name="db">The db.</param>
        /// <param name="date">The date.</param>
        /// <returns></returns>
          public static DataTable GetValues(string url, string token, string db, string date)
        {
            XDocument doc = XDocument.Load(url + "token=" + token + "&db=" + db + "&date=" + date);
            
            DataTable dt1 = new DataTable();
            dt1.Columns.Add("Id", typeof(string));
            dt1.Columns.Add("Description", typeof(string));
            dt1.Columns.Add("Sru", typeof(string));
            dt1.Columns.Add("Group", typeof(string));
            dt1.Columns.Add("Active", typeof(string));
            dt1.Columns.Add("Project", typeof(string));
            dt1.Columns.Add("CostCenter", typeof(string));
            dt1.Columns.Add("Transaction", typeof(string));
            dt1.Columns.Add("Projectvalue", typeof(string));
            dt1.Columns.Add("CostCenterValue", typeof(string));
            dt1.Columns.Add("Transinfovalue", typeof(string));
            dt1.Columns.Add("IB", typeof(string));

            IEnumerable<XElement> Posts = from post in doc.Descendants("account")

                                          //where (string)post.Attribute("id").Value == "1"
                                          select post;

                foreach (XElement Post in Posts)
                {
                    DataRow dr1 = dt1.NewRow();

                  
                    if (Post.Element("id") != null)
                    {
                        dr1["id"] = Post.Element("id").Value;
                    }
                    else
                    {
                        dr1["id"] = "";
                    }

                    if (Post.Element("description") != null)
                    {
                        dr1["Description"] = Post.Element("description").Value;
                    }
                    else
                    {
                        dr1["Description"] = "";
                    }

                    if (Post.Element("sru") != null)
                    {
                        dr1["Sru"] = Post.Element("sru").Value;
                    }
                    else
                    {
                        dr1["Sru"] = "";
                    }

                    if (Post.Element("group") != null)
                    {
                        dr1["Group"] = Post.Element("group").Value;
                    }
                    else
                    {
                        dr1["Group"] = "";
                    }

                    if (Post.Element("active") != null)
                    {
                        dr1["Active"] = Post.Element("active").Value;
                    }
                    else
                    {
                        dr1["Active"] = "";
                    }

                    if (Post.Element("project") != null)
                    {
                        dr1["Project"] = Post.Element("project").Value;
                    }
                    else
                    {
                        dr1["Project"] = "";
                    }

                    if (Post.Element("cc") != null)
                    {
                        dr1["CostCenter"] = Post.Element("cc").Value;
                    }
                    else
                    {
                        dr1["CostCenter"] = "";
                    }

                    if (Post.Element("transaction") != null)
                    {
                        dr1["Transaction"] = Post.Element("transaction").Value;
                    }
                    else
                    {
                        dr1["Transaction"] = "";
                    }

                    if (Post.Element("projectvalue") != null)
                    {
                        dr1["Projectvalue"] = Post.Element("projectvalue").Value;
                    }
                    else
                    {
                        dr1["Projectvalue"] = "";
                    }

                    if (Post.Element("ccvalue") != null)
                    {
                        dr1["CostCenterValue"] = Post.Element("ccvalue").Value;
                    }
                    else
                    {
                        dr1["CostCenterValue"] = "";
                    }

                    if (Post.Element("transinfovalue") != null)
                    {
                        dr1["Transinfovalue"] = Post.Element("transinfovalue").Value;
                    }
                    else
                    {
                        dr1["Transinfovalue"] = "";
                    }

                    if (Post.Element("ib") != null)
                    {
                        dr1["IB"] = Post.Element("ib").Value;
                    }
                    else
                    {
                        dr1["IB"] = "";
                    }

                    dt1.Rows.Add(dr1);
                }

                return dt1;
        }
    }
}
